<div class="layui-fluid">
    <div class="layui-card">
        <div id="body-div" class="layui-card-body">
        	<form id="searchForm" class="searchForm form-inline">
        		<div class="searchDiv positionSpc">
					<div class="form-group pull-left important">
						<span>消息标题</span>
						<input name="title" component-type="input" class="form-control" type="text" value="" maxlength="255" />
					</div>
					<!--<div class="form-group pull-left ">
						<span>发送时间</span>
						<input name="beginSendTime" component-type="datePicker" readonly="readonly" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',maxDate:'#F{$dp.$D(\'endD11\')}'})" id="beginD11" class="Wdate form-control" type="text" value="" />
					</div>
					<div class="form-group pull-left">
						<span>至</span>
						<input name="endSendTime" component-type="datePicker" readonly="readonly" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',minDate:'#F{$dp.$D(\'beginD11\')}'})" id="endD11" class="Wdate form-control" type="text" value="" />
					</div>-->
					<div class="form-group pull-left ">
						<span>状态</span>
						<select name="status" component-type="select" component-id="message_type" class="form-control">
							<option value=""></option>
						</select>
					</div>
				</div>
        	</form>
        	<div id="toolbar" class="toolbar">
				<div class="pull-right">
					<a id="searchBtn" class="btn btn-success btn-bordered btn-sm"><i class="fa fa-search"></i> <font>查询</font></a>
					<a id="resetBtn" class="btn btn-success btn-bordered btn-sm"><i class="fa fa-refresh"></i> <font>重置</font></a>
					<a id="easySearch" class="btn btn-success btn-bordered btn-sm" style="display: none;"><i class="fa fa-backward"></i> <font>返回</font></a>
					<a id="hardSearch" class="btn btn-success btn-bordered btn-sm" style="display: inline-block;"><i class="fa fa-search-plus"></i> <font>高级查询</font></a>
				</div>
			</div>
            <form id="editTableForm">
				<table id="table-list"></table>
			</form>
        </div>
    </div>
</div>
<script>
    layui.use(['common', 'view', 'admin', 'laytpl'], function () {
        var $ = layui.$
            , R = layui.common
            , view = layui.view
            , admin = layui.admin
            , laytpl = layui.laytpl;

        var getTime = "-" + new Date().getTime();
        $("#body-div").attr("id", "body-div" + getTime);
        $("#table-list").attr("id","table-list" + getTime);
        var btnsStr = LayerUtil.getBtnsStrFromMenuData();

		var bootstrapTableUtil = new BootstrapTableUtil(getTime);
		bootstrapTableUtil.init({
			url: admin.basePath + '/system/sysMsg/data',
			method: 'post',
			columns: [
				{
					field: 'title',
					title: '消息标题',
					sortable: true,
					sortName: 'a.title',
					
					formatter:function(value, row , index){
						if(!value){
							value = "";
						}
						return "<a class='read' href='javascript:;' lay-href='"+row.menuHref+"'>"+value+"</a>";
					},
					events: {
						'click .read': function (e, value, row, index) {
							setRead(row);
						}
					},
				},
				{
					field: 'sender',
					title: '发送人',
					sortable: true,
					sortName: 'a.sender',
					
					formatter:function(value, row, index){
						if(value == null){
							return "系统";
						} else {
							return value;
						}
					},
				},
				{
					field: 'sendTime',
					title: '发送时间',
					sortable: true,
					sortName: 'a.send_time',
					
					formatter:function(value, row, index){
						return (typeof value == "undefined" || value == null || value == "") ? "" : moment(value).format('yyyy-MM-dd HH:mm:ss'.replace(/yy/g,"YY").replace(/dd/g,"DD"));
					},
				},
				{
					field: 'readTime',
					title: '阅读时间',
					sortable: true,
					sortName: 'a.read_time',
					
					formatter:function(value, row, index){
						return (typeof value == "undefined" || value == null || value == "") ? "" : moment(value).format('yyyy-MM-dd HH:mm:ss'.replace(/yy/g,"YY").replace(/dd/g,"DD"));
					},
				},
				{
					field: 'status',
					title: '状态',
					sortable: true,
					sortName: 'a.status',
					
					formatter:function(value, row , index){
						return myHelper.getValueFromDictionary("message_type",value);
					},
				},
                {
                	title: layui.admin.lang() ? 'Operation' : '操作',
                	width: '45px',
					"class": 'center must',
					events: {
						'click .read': function (e, value, row, index) {
							setRead(row);
						},
					},
					formatter: function(value, row, index) {
						return  '<div class="show-btn-group">'+
							'<i class="fa fa-cog" aria-hidden="true"></i>'+
							'<div class="hide-btn-group" style="width: 240px;">'+
								'<a class="btn read" href="javascript:;" lay-href="'+row.menuHref+'">查看</a>'+
							'</div>'+
						'</div>';
					},
                },
            ],
		},"oa_sys_msg");

		$("#body-div"+getTime).find("#readAll").on("click",function(){
			setReadAll();
		});
		
		function setRead(row){
    		LayerUtil.ajax({
				url: admin.basePath + '/system/sysMsg/setRead?id=' + row.id,
				type:'post',
        		shade:true,
				success: function(res){
					getMsgCount();
            		bootstrapTableUtil.refresh();
        		}
			});
    	}
		
		function setReadAll(){
    		LayerUtil.ajax({
				url: admin.basePath + '/system/sysMsg/setReadAll',
				type:'post',
        		shade:true,
				success: function(res){
					getMsgCount();
            		bootstrapTableUtil.refresh();
        		}
			});
    	}
		
		function getMsgCount(){
			LayerUtil.ajax({
				type:"get",
				url: admin.basePath + "/system/sysMsg/getUnreadCount",
				success:function(data){
					var count = parseInt(data.total);
					if(count <= 99){
						$("#msgCount").html(count);
					}else{
						$("#msgCount").html("99+");
					}
				}
			});
		}
    });
</script>
